package nl.haemers.testrunners 
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.utils.Timer;
	import nl.haemers.base.BaseMovieClip;
	import nl.haemers.components.grid.Grid;
	import nl.haemers.components.grid.GridItem;
	import nl.haemers.components.layout.TileLayout;
	import flash.events.Event;
	import nl.haemers.test.griditem.CustomGridItem;
	import nl.haemers.components.grid.IGridItem;
	import nl.haemers.utils.Logger;
	import nl.haemers.timer.StopWatch;
	/**
	 * ...
	 * @author Jonathan Haemers
	 */
	public class GridRunner extends BaseMovieClip
	{
		public var grid				:Grid;
		
		private var _timer			:StopWatch;
		
		public function GridRunner() 
		{
		}
		
		override protected function init(e:Event = null) : void
		{
			super.init();
			start();
		}
		
		public function start() : void
		{
			_timer = new StopWatch();
			_timer.start();
			createGrid();
			_timer.stop();
			
			Logger.i(_timer.currentTime.toString() + " ms");
			//configListeners();
		}
		
		protected function createGrid() : void
		{
			_items = createGridItems();
			var grid:Grid = new Grid();
			grid.columns = 50;
			grid.layoutManager = new TileLayout(0, 0);
			
			_items = grid.generateGrid(_items);
			
			_gridRender = new Sprite();
			
			addChild(_gridRender);
			renderItems();
			_gridRender.hitArea = createHitArea();
			_gridRender.hitArea.visible = false;
			_gridRender.useHandCursor = true;
			configListeners();
		}
		
		private function createHitArea() : Sprite
		{
			var spr:Sprite = new Sprite();
			spr.graphics.beginFill(0x00FF00, 1);
			spr.graphics.drawRect(_gridRender.x, _gridRender.y, _gridRender.width, _gridRender.height);
			spr.graphics.endFill();
			
			return spr;
		}
		private var _gridRender:Sprite;
		private var _items:Vector.<GridItem>;
		private function renderItems() : void
		{
			_gridRender.graphics.clear();
			trace("Render: ");
			for each(var item:IGridItem in _items)
			{
				(item as CustomGridItem).render(_gridRender);
			}
		}
		
		private function createGridItems() : Vector.<GridItem>
		{
			var items:Vector.<GridItem> = new Vector.<GridItem>();
			var item:CustomGridItem;
			for (var i:int = 0; i < 1400; i++)
			{
				item = new CustomGridItem( { id: i }, 10, 10);
				items.push(item);
			}
			
			return items;
		}
		
		protected function configListeners() : void
		{
			_gridRender.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			_gridRender.addEventListener(MouseEvent.MOUSE_UP, mouseDownHandler);
			
			/*
			this.stage.addEventListener(MouseEvent.ROLL_OVER, rollOverHandler);
			this.stage.addEventListener(MouseEvent.ROLL_OUT, rollOutHandler);
			*/
			//this.stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}
		
		private function mouseDownHandler(e:MouseEvent) : void
		{
			this.addEventListener(Event.ENTER_FRAME, enterFrameDragging);
		}
		
		private function enterFrameDragging(e:Event) : void
		{
			
		}
		
		private function rollOverHandler(e:MouseEvent) : void
		{
			trace("RollOver");
			this.stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}
		
		private function rollOutHandler(e:MouseEvent) : void
		{
			this.stage.removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
		}
		private var _currentItem:CustomGridItem;
		private function enterFrameHandler(e:Event) : void
		{
			var item:GridItem;
			//trace("EnterFrame");
			for each(item in _items)
			{
				if (item.rect.contains(this.mouseX, this.mouseY))
				{
					if (!_currentItem || _currentItem.gridId != item.gridId)
					{
						if (_currentItem)
							_currentItem.hideBackground();
						_currentItem = item as CustomGridItem;
						_currentItem.showBackground();
					}
					break;
				}
			}
		}
		
	}

}